﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fuzzy
{
    public class xml_class
    {
        private String path;

                
        //CREATING GETTER AND SETTER FOR PATH STRING
       
        public string Path
        {
            get
            {
                return path;
            }
            set
            {
                path = value;
            }
        }

            
        //FUNCTION TO CREATE INITIAL XML FILE USING PATH
        public void createFile()
        {

            XmlDocument xdoc = new XmlDocument();
            XmlNode node = xdoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xdoc.AppendChild(node);

            XmlElement ele = xdoc.CreateElement("Stocks");
            xdoc.AppendChild(ele);
            xdoc.Save(path);
            
        }
        //FUNCTION TO ADD DATA IN XML FILE
        public void adddata(addrecord obj)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlElement[] ele = new XmlElement[9];
            XmlElement company;
            ele[0] = doc.CreateElement("Name");
            ele[1] = doc.CreateElement("Date");
            ele[2] = doc.CreateElement("Opening");
            ele[3] = doc.CreateElement("Closing");
            ele[4] = doc.CreateElement("Dividend");
            ele[5] = doc.CreateElement("Eps");
            ele[6] = doc.CreateElement("Grading");
            ele[7] = doc.CreateElement("Volume");
            ele[8] = doc.CreateElement("Sales");

            company = doc.CreateElement("Company");
            for (int i = 0; i < 9; i++)
            {
                ele[i].InnerText = obj.text(i).ToString();
                company.AppendChild(ele[i]);
            }

            doc.DocumentElement.InsertAfter(company, doc.DocumentElement.LastChild);
            doc.Save(path);

            
        }
        //FUNCTION TO DELETE DATA FROM XML FILE

        public void deletedata(string namedel)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(path);

            XmlNodeList nodelist = doc.GetElementsByTagName("Company");
            string s=null;
            for (int i = 0; i < nodelist.Count; i++)
            {
                s = nodelist[i].ChildNodes[0].InnerText.ToString();
                if (namedel.ToString() == s.ToString())
                    nodelist[i].ParentNode.RemoveChild(nodelist[i]);
            }
            doc.Save(path);
            
        }

    }
}
